<?php

/**
 * reclamo actions.
 *
 * @package    aeropostal
 * @subpackage reclamo
 * @author     Your name here
 */
class reclamoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
        $pager = new sfPropelPager('Reclamo', sfConfig::get('app_max_boletas'));
        $c = new Criteria;
        if($this->getUser()->hasCredential(array('analista'),false)){
          $in_query = 'reclamo.numero IN (SELECT clave FROM log where estructura like "Reclamo" and usuario="'.$this->getUser().'")';
          $c->add(ReclamoPeer::NUMERO, $in_query, Criteria::CUSTOM);  
        }
	$pager->setCriteria($c);
	// Se establece que la pagina principal del index sera la 1
	$pager->setPage($this->getRequestParameter('page', 1));
	$pager->init();
	 // Se Prepara la informaci�n para usarla en la vista.
	$this->pager = $pager;
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->Reclamo = ReclamoPeer::retrieveByPk($request->getParameter('numero'));
    $this->forward404Unless($this->Reclamo);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ReclamoForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ReclamoForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Reclamo = ReclamoPeer::retrieveByPk($request->getParameter('numero')), sprintf('Object Reclamo does not exist (%s).', $request->getParameter('numero')));
    $this->form = new ReclamoForm($Reclamo);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($Reclamo = ReclamoPeer::retrieveByPk($request->getParameter('numero')), sprintf('Object Reclamo does not exist (%s).', $request->getParameter('numero')));
    $this->form = new ReclamoForm($Reclamo);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($Reclamo = ReclamoPeer::retrieveByPk($request->getParameter('numero')), sprintf('Object Reclamo does not exist (%s).', $request->getParameter('numero')));
    $Reclamo->delete();
    $numero = $request->getParameter('numero');
    LogPeer::logBorrar($numero,$this->getUser(),'Reclamo');
    $this->redirect('reclamo/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
        try{
           $Reclamo = $form->save();
           $numero = $request->getPostParameter('reclamo[numero]');
           LogPeer::log($numero,$this->getUser(),'Reclamo');
           $estatus = $request->getPostParameter('reclamo[estatus]');
           if($estatus=='rechazado')
             {
               FacturaPeer::cambiarEstatus($request->getPostParameter('reclamo[numeroFactura]'));
               $numero = $request->getPostParameter('reclamo[numeroFactura]');
               LogPeer::log($numero,$this->getUser(),'Factura');
             }
           $this->redirect('reclamo/index'); 
        }catch (Exception $e){
            $this->getUser()->setFlash('error', 'No se puede registrar este Reclamo, el número de reclamo ya existe.');
            return sfView::NONE;
        }
    }
  }
  
  public function executeMostrarProveedorFechaFactura(sfWebRequest $request)
{
        $factura = $request->getParameter('factura');
        $respuesta = FacturaPeer::doSelectByNumero($factura);
        $this->getResponse()->setContent($respuesta);
        return sfView::NONE;
        
}

  public function executeMostrarEstatus(sfWebRequest $request)
{
        $reclamo = $request->getParameter('reclamo');
        $respuesta = ReclamoPeer::doSelectEstatusByNumero($reclamo);
        $this->getResponse()->setContent($respuesta);
        return sfView::NONE;
        
}
  public function executeCambiarEstatusFactura(sfWebRequest $request)
{
        $factura = $request->getParameter('factura');
        $estatus = $request->getParameter('estatus');
        if($estatus=='rechazado')
        {
            $respuesta = FacturaPeer::cambiarEstatus($factura);
            $this->getResponse()->setContent($respuesta);
        }
        return sfView::NONE;
        
}

}
